;(function ($) {
  $.fn.TreeGrid = function (options_or_method, index) {
    if (!$(this).length) {
      return this;
    }

    if ($.isPlainObject(options_or_method)) {
      //如果是个初始化
      return this.each(function () {
        var id = $(this).attr('id');
        init(id, options_or_method);
      });
    } else {
      //如果是一个方法
      var id = $(this).attr('id');
      return do_method(id, options_or_method, index);
    }
  }

  $.fn.TreeGrid.data = [];

  function init(id, options) {
    var _this = this;
    //默认设置
    var default_settings = {
      id: '',
      url: '',
      rows: {},
      row_index: [],
      status: false,
      field: 'title',
      param: {},
      load_after_init: true,
      row_style: function () {},
      load_handel: null,
    }
    $.fn.TreeGrid.data[id] = $.extend(default_settings, options);

    if ($.fn.TreeGrid.data[id].load_after_init) {
      load_table_data(id);
    }
  }

  //方法
  function do_method(id, method, index) {
    switch (method) {
      case 'getrow' :
        var rows = $.fn.TreeGrid.data[id].rows;
        return get_row_by_id(rows, index);
      case 'getparam' :
        return $.fn.TreeGrid.data[id].param;
      case 'param' :
        return $.fn.TreeGrid.data[id].param = index;
      case 'reload' :
        load_table_data(id);
        break;
    }
  }

  function get_row_by_id(rows, id) {
    for (var i = 0; i < rows.length; i++) {
      var row = rows[i];
      if (row.id == id) {
        return row;
      }

      if ( isset(row['son']) ) {
        ret =  get_row_by_id (row.son , id);
        if ( !empty( ret ) ) {
          return ret ;
        }
      }
    }
  }

  //加载表内容
  function load_table_data(id) {
    var opt = $.fn.TreeGrid.data[id];
    var post_data = {
      page: opt.page,
      page_size: opt.page_size,
      sort: opt.sort,
      order: opt.order,
    };

    $.extend(post_data, $.fn.TreeGrid.data[id].param);

    $.post($.fn.TreeGrid.data[id].url, post_data, function (ret) {

      $.fn.TreeGrid.data[id].status = true;
      $.fn.TreeGrid.data[id].rows = ret.rows;

      var data = $.fn.TreeGrid.data[id];
      var tableobj = $("#" + id);
      if (tableobj.find('tbody').length == 0) {
        tableobj.append('<tbody></tbody>');
      } else {
        tableobj.find('tbody').empty();
      }

      var html = make_table_html(id, data.rows);
      tableobj.find('tbody').append(html);
    });
  }

  //制作 表数据
  function make_table_html(id, rows) {
    var html = '';
    var tableobj = $("#" + id);
    $.each(rows, function (index, row) {
      var tr_c = $.fn.TreeGrid.data[id].row_style(row, index);
      var treefield = $.fn.TreeGrid.data[id].field;
      tr_c = typeof (tr_c) != 'undefinded' && tr_c ? 'class="' + tr_c + '"' : '';
      html += '<tr ' + tr_c + ' >';
      tableobj.find('thead th').each(function () {
        var td_c = '';
        var align = $(this).data('align') ? 'align="' + $(this).data('align') + '"' : '';
        var valign = $(this).data('valign') ? 'valign="' + $(this).data('valign') + '"' : '';
        var field = $(this).data('field') ? $(this).data('field') : '';
        var value = '';

        if (field != '') {
          value = row[field];
          //z(field);
          if ( field == treefield ) {
            td_c = 'class="level-' + row['level'] + '"';
            var icon = typeof (row['son']) != 'undefined' ? '<i class="fa fa-chevron-down"></i> ' : '<i class="fa fa-file"></i> ';
            value = icon + value;
          }
        }

        //var value = field != '' && row[field] ? row[field] : '';   

        if ($(this).data('formatter')) {
          rowformater = eval($(this).data('formatter'));
          value = rowformater(value, row, index);
          value = typeof (value) != 'undefined' ? value : '';
        }
        html += '<td ' + td_c + ' ' + align + ' ' + valign + '>' + value + '</td>';
      });
      html += '</tr>';

      if (typeof (row['son']) != 'undefined') {
        html += make_table_html(id, row['son']);
      }
    })

    return  html;
  }

})(jQuery);